package com.ruge.util;


import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.util.Timeout;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


public class CrawlerHttpUtil {
    // 模拟浏览器User-Agent
    private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36";
    // 请求超时时间（毫秒）
    private static final int TIME_OUT = 15000;

    /**
     * 创建配置好的HttpClient实例
     */
    public static CloseableHttpClient createHttpClient() {
        // 配置请求超时
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(Timeout.ofMilliseconds(TIME_OUT))
                .setResponseTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
                .setConnectionRequestTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
                .build();

        // 设置默认请求头
        List<BasicHeader> headers = new ArrayList<>();
        headers.add(new BasicHeader("User-Agent", USER_AGENT));
        headers.add(new BasicHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"));
        headers.add(new BasicHeader("Accept-Language", "zh-CN,zh;q=0.9"));
        headers.add(new BasicHeader("Referer", "https://baike.baidu.com/"));

        return HttpClients.custom()
                .setDefaultRequestConfig(requestConfig)
                .setDefaultHeaders(headers)
                .build();
    }

    /**
     * 发送HTTP GET请求，获取页面HTML内容
     * HttpClient 5.5中HttpGet不实现AutoCloseable，不需要放入try-with-resources
     */
    public static String getHtmlContent(CloseableHttpClient httpClient, String url) throws Exception {
        // 创建HttpGet对象（不放入try-with-resources）
        HttpGet httpGet = new HttpGet(url);

        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            // 检查响应状态码
            int statusCode = response.getCode();
            if (statusCode != 200) {
                throw new RuntimeException("请求失败，状态码：" + statusCode + "，URL：" + url);
            }

            // 提取响应体并转换为UTF-8编码
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, StandardCharsets.UTF_8);
        }
        // HttpClient 5.x中，不需要手动释放HttpGet连接
        // 响应已经通过try-with-resources关闭，连接会自动归还给连接池
    }

    /**
     * 模拟人类操作的延迟
     */
    public static void delay(int seconds) throws InterruptedException {
        TimeUnit.SECONDS.sleep(seconds);
    }
}
